// 验证服务器有效性模块
const config = require('../config')
const sha1 = require('sha1')

module.exports = () => {
  return (req,res,next)=>{
    console.log(req.query)

    //解构赋值
    const {signature,echostr,timestamp,nonce} = req.query;
    const {token} = config

    const arr = [timestamp,nonce,token];
    const arrSort = arr.sort();
    // console.log(arrSort)
    const str = arr.join('')
    // console.log('str={}',str)
    const sha1Str = sha1(str)
    // console.log('sha1Str={}',sha1Str)
    if(sha1Str === signature){
      console.log('一样')
      //如果一样，返回echostr给微信服务器，contentType，只有这种返回才能配置成功
      res.contentType('text/plain;charset=utf-8')
      res.send(echostr)
    }else {
      res.end('error')
    }
  }
}
